class MyStack {
public:
queue<int> s1;
queue<int> s2;
    MyStack() {

    }
    
    void push(int x) {
if(!s1.empty())
        {
            s1.push(x);
        }
        else
        {
            s2.push(x); 
        }
    }
    
    int pop() {
queue<int>* emptyQ = &s1;
        queue<int>* nonemptyQ = &s2;
        if(!s1.empty())
        {
            swap(emptyQ,nonemptyQ);
        }
        while(nonemptyQ->size() > 1)  
        {
            emptyQ->push(nonemptyQ->front());
            nonemptyQ->pop();
        }
        int top = nonemptyQ->front();
        nonemptyQ->pop();
        return top;
    }
    
    int top() {
if(!s1.empty())
        {
            return s1.back();
        }
        else
        {
            return s2.back(); 
        }
    }
    
    bool empty() {
return s1.empty() && s2.empty();
    }
};